package edu.upf.opencities.dao.connection;

import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;

import com.google.gson.Gson;

import edu.upf.opencities.datamodel.Extras;
import edu.upf.opencities.datamodel.Resource;
import edu.upf.opencities.datamodel.Resources;
import edu.upf.opencities.datamodel.Service;

public class CKANServiceConnectorImpl implements CKANServiceConnector {

	@Override
	public String registerService() {

		//First we build the object with the parameters

		Service service = new Service();
		service.setId("TempService123456789");
		service.setName("Temperature Service");
		service.setDescription("This sensor tells everybody the temperature of the Labs Room");
		service.getCkanTags().add("hot");
//		service.getTags().add("hot");
//		service.getTags().add("cold");
		service.setAuthor("UPF Weather Department");
		service.setEmail("weather@upf.edu");
		Resource res1 = new Resource();
		res1.setDescription("REST End Point");
		res1.setFormat("application/xml");
		res1.setUrl("http://opencities/osn/services/TempService123456789/getEntries");
		Resource res2 = new Resource();
		res2.setDescription("Twitter Account");
		res2.setFormat("application/html");
		res2.setUrl("http://twitter.com/@TempService123456789");
		service.getResources().add(res1);
		service.getResources().add(res2);
		service.setUpdatePeriod("15 min");
		service.setLicense("cc-by");
		service.setUrl("http://opencities/osn/services/TempService123456789");
		Extras extras = new Extras();
		extras.setGeographicalGranularity("meters");
		extras.setTemporalCoverageFrom("Monday");
		service.setExtras(extras);
		
		

		//Now we convert the object into JSON 
		Gson gson = new Gson();
		String jsonObj = gson.toJson(service);

		//We prepare the REST CALL
		WebClient client = WebClient.create("http://www.google.com/api/rest/package");
		//We add the headers
		String authorizationHeader =
			org.apache.cxf.common.util.Base64Utility.
				encode("<MY CKAN API KEY>".getBytes());
		client.header("Authorization", authorizationHeader);
		client.header("Content-Type", "application/json;charset=utf-8");
		
		//Finally we POST the JSON Object
		Response response = client.post(jsonObj);
		int status = response.getStatus();
		return String.valueOf(status+" : "+jsonObj);
		

	}

	@Override
	public String updateService() {
		
		//First we build the object with the parameters to be modified
		String jsonObj = "{\"author\":\"The NEW Measurement Group\"} ";

		//We prepare the REST CALL
		WebClient client = WebClient.create("http://www.google.com/api/rest/package/sensorX");
		//We add the headers
		String authorizationHeader =
			org.apache.cxf.common.util.Base64Utility.
				encode("<MY CKAN API KEY>".getBytes());
		client.header("Authorization", authorizationHeader);
		client.header("Content-Type", "application/json;charset=utf-8");
		
		//Finally we PUT the JSON Object
		Response response = client.put(jsonObj);
		int status = response.getStatus();
		return String.valueOf(status);
	}

	@Override
	public String deleteService() {
		//First we build the object with the parameters to be modified/deleted
		String jsonObj = "{\"state\":\"deleted\"}";

		//We prepare the REST CALL
		WebClient client = WebClient.create("http://www.google.com/api/rest/package/sensorX");
		//We add the headers
		String authorizationHeader =
			org.apache.cxf.common.util.Base64Utility.
				encode("<MY CKAN API KEY>".getBytes());
		client.header("Authorization", authorizationHeader);
		client.header("Content-Type", "application/json;charset=utf-8");
		
		//Finally we PUT the JSON Object
		Response response = client.put(jsonObj);
		int status = response.getStatus();
		return String.valueOf(status);
	}

}
